package cn.xz.mxxz.dialog

import android.text.Editable
import android.text.TextWatcher
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.xz.mxxz.R
import cn.xz.mxxz.adapter.LeaderAdapter
import cn.xz.mxxz.base.BaseApplication
import cn.xz.mxxz.base.BaseDialogRxFragment
import cn.xz.mxxz.bean.LeaderData
import cn.xz.mxxz.util.gone
import cn.xz.mxxz.util.visible

/**
 * 选择领导弹窗
 * */
class LeaderDialog: BaseDialogRxFragment() {

    private var mClickListener: OnClickListener? = null

    private var rvIdList: RecyclerView? = null
    private var tvYes: TextView? = null
    private var tvTxt: TextView? = null
    private var tvNo: TextView? = null
    private var etSearch: EditText? = null
    private var viewQc: View? = null

    private var mDD = true
    private var mLeader: ArrayList<LeaderData> = arrayListOf()
    private var mAdapter: LeaderAdapter? = null

    companion object {
        fun newInstance(
            mLeader: ArrayList<LeaderData>,
            mDD: Boolean,
            clickListener: OnClickListener
        ): LeaderDialog {
            val dialog = LeaderDialog()
            dialog.mLeader = mLeader
            dialog.mDD = mDD
            dialog.mClickListener = clickListener
            return dialog
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_leader_select
    }

    override fun configDialog() {
        this.setDialogSize(gravity = Gravity.CENTER,width = 400)
    }

    override fun initViews() {

        dialog?.let {
            tvTxt = it.findViewById(R.id.tv_txt)
            rvIdList = it.findViewById(R.id.rv_ld_list)
            tvYes = it.findViewById(R.id.tv_yes)
            tvNo = it.findViewById(R.id.tv_no)
            etSearch = it.findViewById(R.id.et_search)
            viewQc = it.findViewById(R.id.view_qc)
        }

        //点击外部不隐藏
        /*dialog?.setCancelable(false)
        dialog?.setCanceledOnTouchOutside(false)*/
        //视图相关
        initUI()
        //点击事件
        initEvent()
    }

    private fun initUI(){
        var str = "带队领导"
        if (!mDD){
            str = "陪同和随从领导"
        }
        tvTxt?.text = "请选择${str}"

        val ms = LinearLayoutManager(BaseApplication.getContext())
        ms.orientation = LinearLayoutManager.VERTICAL
        rvIdList?.layoutManager = ms
        mAdapter = LeaderAdapter()
        mAdapter?.setOnItemClickListener { adapter, view, position ->
            val data = adapter.data as ArrayList<LeaderData>
            data[position].select = !data[position].select
            adapter.notifyDataSetChanged()
        }
        rvIdList?.adapter = mAdapter
        mAdapter?.setList(mLeader)
    }

    private fun initEvent(){
        tvYes?.setOnClickListener {
            val mShow: ArrayList<String> = arrayListOf()
            if (mAdapter != null){
                val data = mAdapter!!.data as ArrayList<LeaderData>
                data.forEach {
                    if (it.select){
                        mShow.add(it.name)
                    }
                }
            }
            mClickListener?.onYes(mShow)
            dismiss()
        }
        tvNo?.setOnClickListener {
            mClickListener?.onNo()
            dismiss()
        }
        etSearch?.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s.toString().isEmpty()){
                    viewQc?.gone()
                    onSearch("")
                }else{
                    viewQc?.visible()
                    onSearch(s.toString())
                }
            }

            override fun afterTextChanged(s: Editable?) {
            }

        })
        viewQc?.setOnClickListener {
            etSearch?.setText("")
        }
    }

    private fun onSearch(s: String){
        //筛选
        if (mAdapter != null){
            val mS: ArrayList<LeaderData> = arrayListOf()
            if (s.isNotEmpty()){
                mLeader.forEach {
                    if (it.name.contains(s)){
                        mS.add(it)
                    }
                }
                mAdapter!!.setList(mS)
            }else{
                mAdapter!!.setList(mLeader)
            }
        }
    }

    interface OnClickListener {
        fun onYes(mLeader: ArrayList<String>)
        fun onNo()
    }

}